﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000237 RID: 567
	[RequireComponent(typeof(Animator))]
	[RequireComponent(typeof(EntityBase))]
	public class CharacterEditor : MonoBehaviour
	{
		// Token: 0x17000163 RID: 355
		// (get) Token: 0x06000BFD RID: 3069 RVA: 0x00063199 File Offset: 0x00061399
		// (set) Token: 0x06000BFE RID: 3070 RVA: 0x000631A1 File Offset: 0x000613A1
		public SkeletonEdit SkeletonEdit { get; private set; }

		// Token: 0x17000164 RID: 356
		// (get) Token: 0x06000BFF RID: 3071 RVA: 0x000631AA File Offset: 0x000613AA
		// (set) Token: 0x06000C00 RID: 3072 RVA: 0x000631B2 File Offset: 0x000613B2
		public MaterialEdit MaterialEdit { get; private set; }

		// Token: 0x17000165 RID: 357
		// (get) Token: 0x06000C01 RID: 3073 RVA: 0x000631BB File Offset: 0x000613BB
		// (set) Token: 0x06000C02 RID: 3074 RVA: 0x000631C3 File Offset: 0x000613C3
		public WeldTool WeldTool { get; private set; }

		// Token: 0x17000166 RID: 358
		// (get) Token: 0x06000C03 RID: 3075 RVA: 0x000631CC File Offset: 0x000613CC
		// (set) Token: 0x06000C04 RID: 3076 RVA: 0x000631D4 File Offset: 0x000613D4
		public Animator Animator { get; private set; }

		// Token: 0x17000167 RID: 359
		// (get) Token: 0x06000C05 RID: 3077 RVA: 0x000631DD File Offset: 0x000613DD
		// (set) Token: 0x06000C06 RID: 3078 RVA: 0x000631E5 File Offset: 0x000613E5
		public string SkeletonHashCode { get; private set; }

		// Token: 0x17000168 RID: 360
		// (get) Token: 0x06000C07 RID: 3079 RVA: 0x000631EE File Offset: 0x000613EE
		// (set) Token: 0x06000C08 RID: 3080 RVA: 0x000631F6 File Offset: 0x000613F6
		public string FolderName { get; private set; }

		// Token: 0x17000169 RID: 361
		// (get) Token: 0x06000C09 RID: 3081 RVA: 0x000631FF File Offset: 0x000613FF
		// (set) Token: 0x06000C0A RID: 3082 RVA: 0x00063207 File Offset: 0x00061407
		public string FolderPath { get; private set; }

		// Token: 0x1700016A RID: 362
		// (get) Token: 0x06000C0B RID: 3083 RVA: 0x00063210 File Offset: 0x00061410
		public Dictionary<string, Transform>.ValueCollection Transforms
		{
			get
			{
				return this.keyTransformMap.Values;
			}
		}

		// Token: 0x06000C0C RID: 3084 RVA: 0x00063220 File Offset: 0x00061420
		private void Awake()
		{
			this.GenerateFileNames();
			this.pathSettings = new XmlResourceSettingReader(true);
			this.GeneratePath();
			this.Animator = base.GetComponent<Animator>();
			if (this.Animator && this.Animator.GetBoneTransform(HumanBodyBones.Hips) != null)
			{
				this.CreateTransformMap(this.Animator.GetBoneTransform(HumanBodyBones.Hips), "0");
			}
			else
			{
				for (int i = 0; i < base.transform.childCount; i++)
				{
					this.CreateTransformMap(base.transform.GetChild(i), i.ToString());
				}
			}
			this.GenerateSkeletonHashCode();
			this.MaterialEdit = base.gameObject.AddComponent<MaterialEdit>();
			this.SkeletonEdit = base.gameObject.AddComponent<SkeletonEdit>();
			this.WeldTool = base.gameObject.AddComponent<WeldTool>();
		}

		// Token: 0x06000C0D RID: 3085 RVA: 0x00063308 File Offset: 0x00061508
		private void GenerateFileNames()
		{
			string model = base.GetComponent<EntityBase>().model;
			string text = string.Empty;
			foreach (string str in model.Split(new char[]
			{
				'/',
				'\\'
			}))
			{
				text += str;
			}
			text = text.Replace(".micro", "-");
			text = text.Replace(".gts", "-");
			this.FolderName = text;
		}

		// Token: 0x06000C0E RID: 3086 RVA: 0x0006338C File Offset: 0x0006158C
		private void GeneratePath()
		{
			string text = this.pathSettings.CharacterPath;
			if (text == null || text.Equals(string.Empty))
			{
				text = Application.persistentDataPath + Path.DirectorySeparatorChar + "Character";
			}
			this.FolderPath = string.Concat(new object[]
			{
				text,
				Path.DirectorySeparatorChar,
				this.FolderName,
				Path.DirectorySeparatorChar
			});
		}

		// Token: 0x06000C0F RID: 3087 RVA: 0x00063410 File Offset: 0x00061610
		private void CreateTransformMap(Transform inTransform, string id = "0")
		{
			if (inTransform.GetComponent<EntityBase>() || inTransform.GetComponent<MeshCollider>() || inTransform.GetComponent<NotABone>())
			{
				return;
			}
			this.keyTransformMap.Add(id, inTransform);
			this.transformKeyMap.Add(inTransform, id);
			for (int i = 0; i < inTransform.childCount; i++)
			{
				this.CreateTransformMap(inTransform.GetChild(i), id + "," + i.ToString());
			}
		}

		// Token: 0x06000C10 RID: 3088 RVA: 0x000634A4 File Offset: 0x000616A4
		public string RegisterTransform(Transform newTransform, Transform parent, string seperator = ",", string customKey = null)
		{
			string transformKey = this.GetTransformKey(parent);
			if (transformKey == null)
			{
				return null;
			}
			this.SkeletonEdit.Disable();
			string text;
			if (customKey == null)
			{
				text = transformKey + seperator + parent.childCount.ToString();
				int num = 1;
				while (this.keyTransformMap.ContainsKey(text))
				{
					text = transformKey + seperator + (parent.childCount + num).ToString();
					num++;
				}
			}
			else
			{
				text = transformKey + seperator + customKey;
			}
			this.SkeletonEdit.Enable();
			this.keyTransformMap.Add(text, newTransform);
			this.transformKeyMap.Add(newTransform, text);
			return text;
		}

		// Token: 0x06000C11 RID: 3089 RVA: 0x00063560 File Offset: 0x00061760
		public void DeleteTransform(string key)
		{
			if (!this.keyTransformMap.ContainsKey(key))
			{
				return;
			}
			Transform transform = this.keyTransformMap[key];
			this.keyTransformMap.Remove(key);
			this.transformKeyMap.Remove(transform);
			if (transform.GetComponent<EntityBase>())
			{
				transform.GetComponent<EntityBase>().DestroyObject(true);
			}
			else
			{
				UnityEngine.Object.Destroy(transform.gameObject);
			}
		}

		// Token: 0x06000C12 RID: 3090 RVA: 0x000635D4 File Offset: 0x000617D4
		private void GenerateSkeletonHashCode()
		{
			string s = this.GenerateTransformMapString();
			MD5 md = MD5.Create();
			byte[] array = md.ComputeHash(Encoding.UTF8.GetBytes(s));
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < array.Length; i++)
			{
				stringBuilder.Append(array[i].ToString("x2"));
			}
			this.SkeletonHashCode = stringBuilder.ToString();
		}

		// Token: 0x06000C13 RID: 3091 RVA: 0x00063644 File Offset: 0x00061844
		private string GenerateTransformMapString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (string value in this.keyTransformMap.Keys)
			{
				stringBuilder.Append(value);
				stringBuilder.Append(':');
			}
			return stringBuilder.ToString();
		}

		// Token: 0x06000C14 RID: 3092 RVA: 0x000636BC File Offset: 0x000618BC
		public Transform GetTransform(string key)
		{
			if (this.keyTransformMap.ContainsKey(key))
			{
				return this.keyTransformMap[key];
			}
			return null;
		}

		// Token: 0x06000C15 RID: 3093 RVA: 0x000636DD File Offset: 0x000618DD
		public string GetTransformKey(Transform inTransform)
		{
			if (this.transformKeyMap.ContainsKey(inTransform))
			{
				return this.transformKeyMap[inTransform];
			}
			return null;
		}

		// Token: 0x06000C16 RID: 3094 RVA: 0x000636FE File Offset: 0x000618FE
		private void OnDestroy()
		{
			UnityEngine.Object.Destroy(this.WeldTool);
			UnityEngine.Object.Destroy(this.MaterialEdit);
			UnityEngine.Object.Destroy(this.SkeletonEdit);
		}

		// Token: 0x04001155 RID: 4437
		private XmlResourceSettingReader pathSettings;

		// Token: 0x04001156 RID: 4438
		private Dictionary<Transform, string> transformKeyMap = new Dictionary<Transform, string>();

		// Token: 0x04001157 RID: 4439
		private Dictionary<string, Transform> keyTransformMap = new Dictionary<string, Transform>();
	}
}
